package com.good.child;

import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.good.child.bean.login.LoginBean;
import com.good.child.config.Constants;
import com.good.child.http.LoginLogical;
import com.good.child.iview.MainIView;
import com.good.child.pressent.MainPresenter;
import com.good.child.ui.activity.AccountActivity;
import com.good.child.ui.activity.StudyGardenActivity;
import com.good.child.ui.activity.ai.AiCommunityActivity;
import com.good.child.ui.activity.ai.AiGeneratePictureActivity;
import com.good.child.ui.activity.ai.AiGeneratePictureByImageActivity;
import com.good.child.ui.activity.ai.AiGenerateTextByImageActivity;
import com.good.child.ui.activity.ai.AiMessageListActivity;
import com.good.child.ui.activity.camera.CameraActivity;
import com.good.child.ui.activity.constants.ConstantsListActivity;
import com.good.child.ui.adapter.ItemMeunAdapter;
import com.good.child.ui.service.LocationUpdateService;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.base.ICallBackResultListener;
import com.yimidida.library.base.LibraryApplication;
import com.yimidida.library.tool.TActivityUtils;
import com.yimidida.library.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create on: 2024/2/24
 * by Mr.Zhu
 * Description: TODO
 */
public class MainActivity extends BaseActivity<MainIView, MainPresenter> implements View.OnClickListener {


    //    @BindView(R.id.recyclerview)
//    RecyclerView recyclerView;
    @BindView(R.id.image_ai_camera)//相机
            ImageView aiCamera;
    @BindView(R.id.image_ai_helper)//助手
    ImageView aiHelper;
    @BindView(R.id.image_ai_constants)//联系人
    ImageView aiConstants;
    @BindView(R.id.image_ai_study)//学习
    ImageView aiStudy;
    @BindView(R.id.image_ai_community)//社区
    ImageView aiCommunity;
    @BindView(R.id.image_ai_image)//生成图片
    ImageView aiImage;
    @BindView(R.id.image_ai_image_speak)//图片讲解
    ImageView aiImageSpeak;
    @BindView(R.id.image_ai_generate_ani)//生成动画
    ImageView aiGenerateAni;
    @BindView(R.id.image_ai_settings)//设置
    ImageView aiSettings;
    private int currentSelectedIndex = 0;
    private ImageView[] imageViews;



    private long exitTime = 0;// 双击返回

    private ItemMeunAdapter itemMeunAdapter;
    private List<String> itemList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        imageViews = new ImageView[]{
                findViewById(R.id.image_ai_camera),
                findViewById(R.id.image_ai_helper),
                findViewById(R.id.image_ai_constants),
                findViewById(R.id.image_ai_community),
                findViewById(R.id.image_ai_study),
                findViewById(R.id.image_ai_image),
                findViewById(R.id.image_ai_image_speak),
                findViewById(R.id.image_ai_generate_ani),
                findViewById(R.id.image_ai_settings)
        };

//        String imie=getIMEI();
//        Toast.makeText(MainActivity.this,imie,Toast.LENGTH_LONG);
    }
    private String getIMEI(){
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        int phoneCnt = tm.getPhoneCount();
        String imei = "";

        for (int i = 0; i < phoneCnt; i++) {
            imei += "imei";
            imei += String.valueOf(i + 1);
            imei += ":";
            imei += tm.getDeviceId(i)  ;
            imei += "\n";
        }
        return imei;
    }

    @Override
    protected MainPresenter initPresenter() {
        return new MainPresenter(this);
    }

    @Override
    protected void initView() {
        setStatusBar(true, true, R.color.white);
        setTitle("Home");
        setViewLineVisable(false);
        setAndroidNativeLightStatusBar(this, false);
        LocationUpdateService.restart();
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3); // 指定列数
        itemMeunAdapter = new ItemMeunAdapter(this);
        initData();
        loginDevices();
    }


    private void initData() {
        aiCamera.setOnClickListener(this);
        aiCamera.setSelected(true);
        aiCamera.setFocusable(true);
        aiCamera.setFocusableInTouchMode(true);
        aiHelper.setOnClickListener(this);
        aiConstants.setOnClickListener(this);
        aiStudy.setOnClickListener(this);
        aiCommunity.setOnClickListener(this);
        aiImage.setOnClickListener(this);
        aiImageSpeak.setOnClickListener(this);
        aiGenerateAni.setOnClickListener(this);
        aiSettings.setOnClickListener(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消所有请求
//        OkGo.getInstance().cancelAll();
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkUpdate();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_UP || keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
                if(0==currentSelectedIndex){
                    currentSelectedIndex=8;
                }else {
                    currentSelectedIndex = (currentSelectedIndex - 1) % imageViews.length;
                }
            }else{
                if(8==currentSelectedIndex) {
                    currentSelectedIndex = 0;
                } else {
                    currentSelectedIndex = (currentSelectedIndex + 1) % imageViews.length;
                }
            }
            setSelected(currentSelectedIndex);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void setSelected(int index) {
        for (int i = 0; i < imageViews.length; i++) {
            if (i == index) {
                imageViews[i].setSelected(true);
                imageViews[i].setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
                imageViews[i].setFocusable(true);
                imageViews[i].setFocusableInTouchMode(true);

            } else {
                imageViews[i].setSelected(false);
                imageViews[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));
                imageViews[i].setFocusable(false);
                imageViews[i].setFocusableInTouchMode(false);
            }
        }

    }


    private void checkUpdate() {
        mPresenter.checkUpdate(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_ai_camera:
                TActivityUtils.jumpToActivity(MainActivity.this, CameraActivity.class);
                break;
            case R.id.image_ai_helper:
                TActivityUtils.jumpToActivity(MainActivity.this, AiMessageListActivity.class);
                break;
            case R.id.image_ai_constants:
                TActivityUtils.jumpToActivity(MainActivity.this, ConstantsListActivity.class);
                break;
            case R.id.image_ai_study:
//                ToastUtil.showShort("敬请期待");
                TActivityUtils.jumpToActivity(MainActivity.this, StudyGardenActivity.class);
                break;
            case R.id.image_ai_community:
                TActivityUtils.jumpToActivity(MainActivity.this, AiCommunityActivity.class);
                break;
            case R.id.image_ai_image:
                TActivityUtils.jumpToActivity(MainActivity.this, AiGeneratePictureActivity.class);
                break;
            case R.id.image_ai_image_speak:
               TActivityUtils.jumpToActivity(MainActivity.this, AiGenerateTextByImageActivity.class);
              //  ToastUtil.showShort("敬请期待");
                break;
            case R.id.image_ai_generate_ani:
                TActivityUtils.jumpToActivity(MainActivity.this, AiGeneratePictureByImageActivity.class);
                break;
            case R.id.image_ai_settings:
                TActivityUtils.jumpToActivity(MainActivity.this, AccountActivity.class);
                break;

        }
    }

    private void loginDevices() {
        String telephone = (String) LibraryApplication.getSharedata().getData(Constants.LOGIN_TELEPHONE, "");
        String deviceCode = (String) LibraryApplication.getSharedata().getData(Constants.DEVICE_CODE, "");
        LoginLogical.getInstance().requestAppAutoLogin(this, telephone, deviceCode, new ICallBackResultListener() {
            @Override
            public void onCallBack(Object result) {
                if (result == null) {
                    return;
                }
                LoginBean userInfo = (LoginBean) result;
                if (userInfo == null) {
                    return;
                }
                LibraryApplication.getSharedata().saveData(Constants.LOGIN.TOKEN_ID, userInfo.getAccess_token());
                Constants.token = userInfo.getAccess_token();
            }
        });
    }
}
